package com.zifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zifeng.common.Query;
import com.zifeng.dao.UserDao;
import com.zifeng.pojo.User;
import com.zifeng.service.IUserService;
import com.zifeng.utils.DateUtils;
import com.zifeng.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * 船舶用户Service业务层处理
 * 
 * @author zifeng
 * @date 2025-03-27
 */
@Service("userService")
public class UserServiceImpl  extends ServiceImpl<UserDao, User> implements IUserService
{
    @Autowired
    private UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<User> page = this.page(
                new Query<User>().getPage(params),
                new QueryWrapper<User>()
        );

        return new PageUtils(page);
    }

    @Override
    public Long countByMonth(Integer year, Integer month) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String startDate = String.format("%04d-%02d-01 00:00:00", year, month);
        String endDate = String.format("%04d-%02d-%02d 23:59:59", year, month, DateUtils.getLastDayOfMonth(year, month));
        queryWrapper.between("create_at", startDate, endDate);
        return userDao.selectCount(queryWrapper);
    }

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(Long id)
    {
        return userDao.selectById(id);
    }

    /**
     * 新增用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        return userDao.insert(user);
    }

    /**
     * 修改用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        return userDao.updateById(user);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        return userDao.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userDao.deleteById(id);
    }
}
